import { MockMethod } from "vite-plugin-mock";

// http://mockjs.com/examples.html#Object
const userData = [
  {id:1,account:"admin",password:"123456",userName:"超级管理员",avatar:"https://img1.baidu.com/it/u=1919046953,770482211&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"},
  {id:2,account:"test1",password:"123456",userName:"小红",avatar:"https://img2.baidu.com/it/u=2353293941,2044590242&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"},
  {id:3,account:"test2",password:"123456",userName:"小黑",avatar:"https://img1.baidu.com/it/u=63401368,3081051561&fm=253&fmt=auto&app=138&f=JPEG?w=360&h=360"},
]

// 经过一系列操作生成了accessToken、refreshToken
const createdToken = ()=>{
  return {
    accessToken:"abcdef",
    refreshToken:"qwerty"
  }
}
// 更新token接口中接收到的前端返回的refreshToken,判断refreshToken是否有效，假定这里已获取到前端回传的refreshToken
const isTokenValid = ()=>{
  return true;
}

// 判断accessToken是否有效，假定这里已获取到前端回传的accessToken
const isAccessTokenValid = ()=>{
  return true;
}
// 判断refreshToken是否有效，假定这里后端已经通过accessToken匹配到了refreshToken
const isRefreshTokenValid = ()=>{
  return true;
}
export default [
  {
    url: "/login",
    method: "post",
    response: (data) => {
      var isUser = false;
      var isLogin = false;
      var userInfo = {};
      userData.forEach(function(item,index){
        if(item.account == data.body.account){
          isUser = true;
          if(item.password === data.body.password){
            isLogin = true;
            userInfo = item;
          }
        }
      });
      if(isUser){
        if(isLogin){
          var token = createdToken();
          return {
            code: 200,
            status:true,
            info:"登录成功！",
            data:userInfo,
            accessToken:token.accessToken,
            refreshToken:token.refreshToken,
          }
        }else{
          return {
            code: 200,
            status:false,
            info:"密码错误！",
          }
        }
      }else{
        return {
          code: 200,
          status:false,
          info:"用户名不存在！",
        }
      }
    }
  },
  {
    url: "/refreshToken",
    method: "post",
    response: ({refreshToken}) => {
      // 这里接收前端接口参数中回传的refreshToken，需要判断refreshToken是否也失效了，如果失效了，需要返回登录超时，如果未失效，则返回新的accessToken、expireTime、refreshToken
      if(isTokenValid()){
        var token = createdToken();
        return {
          code: 200,
          status:true,
          info:"token更新成功！",
          data:{
            accessToken:token.accessToken,
            refreshToken:token.refreshToken,
          },
        }
      }else{
        return {
          code: 201002,
          status:false,
          info:"登录已超时，请重新登录！",
          data:{},
        }
      }
    }
  },
  {
    url: "/logout",
    method: "post",
    response: () => {
      if(isRefreshTokenValid()){
        if(isAccessTokenValid()){
          return {
            code: 200,
            status:true,
            info:"退出登录成功！"
          };
        }else{
          return {
            code: 201004,
            status:false,
            info:"token已失效，请更新token！",
            data: []
          };
        }
      }else{
        return {
          code: 201002,
          status:false,
          info:"登录已超时，请重新登录！",
          data: []
        };
      }
    }
  }
] as MockMethod[];